﻿//-----------------------------------------------------------------------
// <copyright file="WorkingNode.cs" company="INL sprl">
//     Copyright 2010 INL sprl.
// </copyright>
// Fabrice avaux : Date de création 17/01/2010
//-----------------------------------------------------------------------

namespace ParseXAML
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;

    /// <summary>
    /// Classe auxiliaire utilisée pour le traitement du noeud en cours.
    /// </summary>
    internal class WorkingNode
    {
        /// <summary>
        /// Noeud en cours de traduction
        /// </summary>
        private XmlElement node;

        /// <summary>
        /// Fichier en cours de traitement
        /// </summary>
        private XAMLFile file;

        /// <summary>
        /// Paramètres globaux
        /// </summary>
        private Global global;

        /// <summary>
        /// Attribut de l'Uid
        /// </summary>
        private XmlAttribute xamlElementUid;

        /// <summary>
        /// Attribut du nom de l'élement 
        /// </summary>
        private XmlAttribute xamlElementName;

        /// <summary>
        /// Initializes a new instance of the WorkingNode class
        /// </summary>
        /// <param name="node">Node to analyse</param>
        /// <param name="file">Document analysed</param>
        /// <param name="global">Global parameters</param>
        internal WorkingNode(XmlElement node, XAMLFile file, Global global)
        {
            this.node = node;
            this.file = file;
            this.global = global;
            this.xamlElementUid = node.GetAttributeNode("Uid", NamespaceAssociations.nsXamlName);
            this.NameUid = null;
            this.NameThis = null;
            ////get Name element (sometimes qualified, sometimes not ;) 
            this.xamlElementName = Utils.GetName(node);
            if (this.xamlElementUid != null)
            {
                this.NameUid = this.xamlElementUid.InnerXml;
            }
            else if (this.xamlElementName != null)
            {
                this.NameUid = this.xamlElementName.InnerXml;
            }

            if (this.xamlElementName != null)
            {
                this.NameThis = this.xamlElementName.InnerXml;
            }
        }

        /// <summary>
        /// Gets or sets l'Uid de l'élement
        /// </summary>
        internal string NameUid { get; set; }

        /// <summary>
        /// Gets or sets du nom de l'élément
        /// </summary>
        internal string NameThis { get; set; }

        /// <summary>
        /// Gets a value indicating whether the node has a name
        /// </summary>
        internal bool HasName
        {
            get
            {
                return this.xamlElementName != null;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the node has a Uid
        /// </summary>
        internal bool HasUid
        {
            get
            {
                return this.xamlElementUid != null;
            }
        }

        /// <summary>
        /// Create a Uid or Name (depend on the global parameters)
        /// </summary>
        /// <param name="inResource">Indicate if the node is in resource node</param>
        internal void CreateUidOrNameIfNecessary(bool inResource)
        {
            bool createUid = this.global.CreateUidAttribute || (inResource && this.global.CreateNameAttribute);
            if ((this.NameUid == null) && createUid)
            {
                if (this.NameThis != null)
                {
                    // try with the name first (if people set Name, it is better to have the same name is resx)
                    this.NameUid = this.file.Uids.GenerateNewName(this.NameThis);
                }
                else
                {
                    this.NameUid = this.file.Uids.GenerateNewName(this.node.LocalName);
                }

                this.file.NumberAddUid++;
                this.node.SetAttribute("Uid", NamespaceAssociations.nsXamlName, this.NameUid);
            }

            if ((this.NameThis == null) && this.global.CreateNameAttribute && !inResource)
            {
                if (this.NameUid != null)
                {
                    // Using same as Uid is better to have Name and Uid different (more readable)
                    this.NameThis = this.file.Names.GenerateNewName(this.NameUid);
                }
                else
                {
                    this.NameThis = this.file.Names.GenerateNewName(this.node.LocalName);
                    this.NameUid = this.NameThis;
                }

                this.file.NumberAddName++;
                this.node.SetAttribute("Name", NamespaceAssociations.nsXamlName, this.NameThis);
            }
        }
    }
}
